package com.qu.ale.fitscreen.widget;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentCallbacks;
import android.content.res.Configuration;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;

import com.qu.ale.utils.DeviceScreen;

/**
 * @描述 dimmension
 * @作者 阿乐
 * @创建时间 2018/7/18 15:32
 * @邮箱 1123599561@qq.com
 */
public class CustomDimmension {
    private static float mDensity;
    private static float mscaledDensity;
    private static float mScreenWidth;
    private static float mScreenHeight;
    private static float mDesignWidth;
    private static float mDesignHeight;
    public static void setCustomDimmension(@Nullable final Application application,
                                     @Nullable Activity activity,@Nullable boolean isFitWidth){
        DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
        if(mDensity == 0){
            float[] sizes = DeviceScreen.getScreenWidthandHeight(application);
            if(isFitWidth){

                mScreenWidth = appDisplayMetrics.widthPixels;
                mDesignWidth = sizes[0];
            }else{

                //适配的高度是包括状态栏、菜单栏
                mScreenHeight = appDisplayMetrics.heightPixels - DeviceScreen.getStatusBarHeight(application);
                mDesignHeight = sizes[1];
            }
            mDensity = appDisplayMetrics.density;
            mscaledDensity = appDisplayMetrics.scaledDensity;
            application.registerComponentCallbacks(new ComponentCallbacks() {
                @Override
                public void onConfigurationChanged(Configuration newConfig) {
                    if(newConfig != null && newConfig.fontScale > 0){
                        mscaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
                    }
                }
                @Override
                public void onLowMemory() {}
            });
        }

        //重新计算Density、DensityDpi、scaleDensity
        float targetDensity;
        int targetDensityDpi;
        float scaledDensity;
        if(isFitWidth) {
            targetDensity = mScreenWidth / mDesignWidth;
            targetDensityDpi = (int) targetDensity * 160;
            scaledDensity = targetDensity * (mscaledDensity / mDensity);
        }else {
            targetDensity = mScreenHeight / mDesignHeight;
            targetDensityDpi = (int) targetDensity * 160;
            scaledDensity = targetDensity * (mscaledDensity / mDensity);
        }

        appDisplayMetrics.density = targetDensity;
        appDisplayMetrics.densityDpi = targetDensityDpi;
        appDisplayMetrics.scaledDensity = scaledDensity;

        final DisplayMetrics activityDisplayMetrics = activity.getResources().getDisplayMetrics();
        activityDisplayMetrics.density = targetDensity;
        activityDisplayMetrics.densityDpi = targetDensityDpi;
        activityDisplayMetrics.scaledDensity = scaledDensity;

    }
}
